package cz.kfad.ofrss.activity;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.actionbarsherlock.app.SherlockActivity;

import cz.kfad.ofrss.R;
import cz.kfad.ofrss.backend.BackendService;
import cz.kfad.ofrss.backend.IBackend;
import cz.kfad.ofrss.common.ApplicationContext;
import cz.kfad.ofrss.model.Channel;
import cz.kfad.ofrss.util.PreDefineChannelsUtil;
import cz.kfad.ofrss.util.StringUtil;

public class AddChannelActivity extends SherlockActivity {

    public static final String CHANEL_ID = "chanel_id";

    private IBackend backend = BackendService.createBackend(this);

    private int channelId;

    private boolean isForEditing;

    private EditText findUrlEditText() {
        return (EditText) findViewById(R.id.channelURL);
    }

    private EditText findNameEditText() {
        return (EditText) findViewById(R.id.channelName);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        channelId = getIntent().getIntExtra(CHANEL_ID, -1);
        isForEditing = channelId != -1;

        setContentView(R.layout.add_channel);
        if (isForEditing) {
            Channel channel = backend.findChannelById(channelId);
            findNameEditText().setText(channel.getName());
            findUrlEditText().setText(channel.getUrl());

            setTitle(R.string.channel_edit_title);
        } else {
            setTitle(R.string.channel_add_title);
        }

        Button predefineButton = (Button) findViewById(R.id.channelPredefineButton);
        predefineButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                final AlertDialog.Builder builder = new AlertDialog.Builder(AddChannelActivity.this);

                builder.setItems(PreDefineChannelsUtil.getPreDefineChannelsString().toArray(new CharSequence[]{}),
                        new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                PreDefineChannelsUtil.PreDefineChannel preDefineChannel = PreDefineChannelsUtil.getOnPosition(which);
                                onPredefineChannelSelected(preDefineChannel);
                            }
                        });
                final AlertDialog alertDialog = builder.create();
                alertDialog.show();
            }
        });


        Button saveButton = (Button) findViewById(R.id.channelSaveButton);
        saveButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                onSaveClick();
            }
        });
    }

    private void onPredefineChannelSelected(PreDefineChannelsUtil.PreDefineChannel preDefineChannel) {
        findNameEditText().setText(preDefineChannel.getName());
        findUrlEditText().setText(preDefineChannel.getUrl());
    }

    @Override
    protected void onResume() {
        super.onResume();
        ApplicationContext appState = ((ApplicationContext) getApplicationContext());
        appState.setContextToAsynTasks(this);
    }

    private void onSaveClick() {
        final Channel channel = new Channel();
        channel.setUrl(findUrlEditText().getText().toString());
        channel.setName(findNameEditText().getText().toString());

        if (validateChannel(channel)) {
            if (isForEditing) {
                channel.setId(channelId);
                backend.editChannel(channel);
            } else {
                backend.saveChannel(channel);
            }
			ApplicationContext appState = ((ApplicationContext) getApplicationContext());
			appState.saveOrUpdate(channel, this);
            finish();
        } else {
            Toast.makeText(this, getString(R.string.channelDefinitionInvalid), Toast.LENGTH_LONG).show();
        }
    }

    private boolean validateChannel(final Channel channel) {
        final String name = channel.getName();
        final String url = channel.getUrl();
		return name != null && url != null && !StringUtil.isEmpty(name) && !StringUtil.isEmpty(url);
    }
}
